/* netsctest.c
 *	Simple program to test using networking to implement lock/cv/mv-related syscalls
 *	
 *	Does syscalls that create, acquire, release, and destroy locks and create, wait, signal, broadcast, and destory CVs, create, set, get, destory MVs
 *  through connection to a server. 
 */
#include "syscall.h"

void LockTest();
void MVTest();
void CVTest();

int main(int argc, char **argv)
{   
	/* Must run tests with -d t to see all error output\n");*/

	LockTest();
	MVTest();
	/*CVTest();*/
	
	/* Then we're done!*/ 
    Halt(); /*MUST do since using networking (post office has own thread)*/
}

void LockTest()
{
    int lockIndex, lockIndex2, i;

    PrintOut("\n\n******************NETWORK LOCK TESTS*******************\n\n", 59); 
  
    /*Correct Use - Should Pass*/
	
	PrintOut("\nTest 1: Using Create Correctly................................\n", 64); 
    lockIndex = CreateLock("BobLock", 7, 1);
	lockIndex2 = CreateLock("TomLock", 7, 1);
	if(lockIndex2 > lockIndex)
	{
		PrintOut("\nTEST 1: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 1: FAILED................................................\n", 64, ConsoleOutput);
	}

	PrintOut("\nTest 2: Using Acquire on newly created lock...................\n", 64);
    if(Acquire(lockIndex2)==0) /*If lock asked to acquire is acquired. 0 - Error code for success*/
	{
		PrintOut("\nTEST 2: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 2: FAILED................................................\n", 64, ConsoleOutput);
	}
	

    PrintOut("\nTest 3: Using Release after a Acquire on same lock............\n", 64);
    if(Release(lockIndex2)==0) /*If lock asked to release is released. 0 - Error code for success*/
	{
		PrintOut("\nTEST 3: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 3: FAILED................................................\n", 64, ConsoleOutput);
	}

    PrintOut("\nTEST 4: Destroying a lock.....................................\n", 64, ConsoleOutput);
	if(DestroyLock(lockIndex2, 1) == 0) /*If lock asked to destroy is destroyed. 0 - Error code for success*/
	{
		PrintOut("\nTEST 4: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 4: FAILED................................................\n", 64, ConsoleOutput);
	}
}

void MVTest()
{
    int mvIndex, mvIndex2, i, value;

    PrintOut("\n\n********************NETWORK MV TESTS*******************\n\n", 59); 
  
    PrintOut("\nTEST 1: Creating 2 MVs........................................\n", 64, ConsoleOutput);
	mvIndex = CreateMV("BobMV", 5, 1);
	mvIndex2 = CreateMV("JimMV", 5, 1);
	if(mvIndex2 > mvIndex)
	{
		PrintOut("\nTEST 1: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 1: FAILED................................................\n", 64, ConsoleOutput);
	}

	PrintOut("\nTEST 2: Setting/Getting an MV after creating it...............\n", 64, ConsoleOutput);
 
	SetMV(mvIndex2, 9); /*first int is index of MV, and second int is value to set it to*/
	value = GetMV(mvIndex2);
	if(value == 9)
	{
		PrintOut("\nTEST 2: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 2: FAILED................................................\n", 64, ConsoleOutput);
	}

	PrintOut("\nTEST 3: Destroying an MV......................................\n", 64, ConsoleOutput);
	if(DestroyMV(mvIndex2, 1) == 0) /*If mv asked to destroy is destroyed. 0 - Error code for success*/
	{
		PrintOut("\nTEST 3: PASSED................................................\n", 64, ConsoleOutput);
	}
	else
	{
		PrintOut("\nTEST 3: FAILED................................................\n", 64, ConsoleOutput);
	}
}

void CVTest()
{
    int lockIndex2, cvIndex, i;

    PrintOut("\n\n*************NETWORK CREATECV TESTS********************\n\n", 59); 
  
    /*Correct Use - Should Pass*/
	
	PrintOut("Test 1: Using Create Correctly.................................\n", 64); 
	
	cvIndex = CreateCV("tempCV", 6, 1);
	lockIndex2 = CreateLock("tempLock", 8, 1);
	

	PrintOut("Test 2: Using Signal on tempLock...............................\n", 64);
    
	PrintOut("Using Correct CV & Lock\n", 24);
	if (Signal(cvIndex, lockIndex2) == 0){
		PrintOut("Signal Succeed!!\n", 17);
	}
	else{
		PrintOut("Signal Failed!!\n", 16);
	}
	
	/*PrintOut("Using Wrong Lock\n", 17);
	if (Signal(cvIndex, -1) == 0){
		PrintOut("Signal Succeed!!\n", 17);
	}
	else{
		PrintOut("Signal Failed!!\n", 16);
	}
	
	PrintOut("Using Wrong CV\n", 15);
	if (Signal(-1,lockIndex2 ) == 0){
		PrintOut("Signal Succeed!!\n", 17);
	}
	else{
		PrintOut("Signal Failed!!\n", 16);
	}*/
	

    PrintOut("Test 3: Using Wait on tempLock.................................\n", 64);
	PrintOut("Using Correct CV & Lock\n", 24);
    if (Wait(cvIndex, lockIndex2) == 0)
		PrintOut("Wait Succeed!!\n", 15); /*Wait on a lock. Returns error code, 0 - SUCCESS*/
	else
		PrintOut("Wait Failed!!\n", 14);

	/*PrintOut("Using Wrong Lock\n", 17);
	if (Wait(cvIndex, -1) == 0){
		PrintOut("Wait Succeed!!\n", 15);
	}
	else{
		PrintOut("Wait Failed!!\n", 14);
	}
	
	PrintOut("Using Wrong CV\n", 15);
	if (Wait(-1,lockIndex2 ) == 0){
		PrintOut("Wait Succeed!!\n", 15);
	}
	else{
		PrintOut("Wait Failed!!\n", 14);
	}*/
	
	PrintOut("Test 4: Using Destroy after a create...........................\n", 64);	
	PrintOut("Using Correct CV\n", 17);
	if (DestroyCV(cvIndex)==0)
		PrintOut("DestroyCV Succeed!!\n", 20);
	else
		PrintOut("DestroyCV Failed!!\n", 19);
		
	/*PrintOut("Using Wrong CV\n", 15);
	if (DestroyCV(-1)==0)
		PrintOut("DestroyCV Succeed!!\n", 20);
	else
		PrintOut("DestroyCV Failed!!\n", 19);*/
}






